/**
 * Represents a record in the DVD inventory
 * 
 * @author Adam Contois, Adam Domurad, Pat Goebel
 * 
 */
public class DVDRecord {
	private String name;
	private int id, remain, total, priceInCents;
	private boolean rentalStatus;

	/**
	 * Create a DVD record
	 * 
	 * @param name
	 *            name of movie for rent or sale
	 * @param remain
	 *            amount of copies available
	 * @param total
	 *            total copies available
	 * @param priceInCents
	 *            price (in cents) of the movie (rent or sale)
	 * @param rentalStatus
	 *            whether the DVD is for rent (true) or for sale (false)
	 */
	public DVDRecord(int id, int remain, int total, boolean rentalStatus,
			int priceInCents, String name) {
		this.id = id;
		this.remain = remain;
		this.total = total;
		this.rentalStatus = rentalStatus;
		this.priceInCents = priceInCents;
		this.name = name;
		// System.out.println("Created DVD Record with name="+name+", total="+total+", price="+FormatUtil.formatPrice(priceInCents)+", rentalStatus="+rentalStatus);
	}

	/**
	 * Returns the DVD record's ID
	 * 
	 * @return the id of the associated movie
	 */
	public int getID() {
		return id;
	}

	/**
	 * Returns the DVD record's name
	 * 
	 * @return the name of the associated movie
	 */
	public String getName() {
		return name;
	}

	/**
	 * Returns the DVD record's amount available
	 * 
	 * @return the amount of DVD's available
	 */
	public int getRemaining() {
		return remain;
	}

	/**
	 * Returns the DVD record's total in circulation
	 * 
	 * @return the amount of DVD's available
	 */
	public int getTotal() {
		return total;
	}

	/**
	 * Returns the DVD record's rental status
	 * 
	 * @return true if a Rental DVD, false if a Sale DVD
	 */
	public boolean getRentalStatus() {
		return rentalStatus;
	}

	/**
	 * Returns the DVD record's price in cents
	 * 
	 * @return the price of the dvd in cents
	 */
	public int getPriceInCents() {
		return priceInCents;
	}

	/**
	 * Sets the amount remaining of this record to copies
	 * 
	 * @param copies
	 *            the number of copies to set to
	 */
	public void setRemaining(int copies) {
		this.remain = copies;
	}

	/**
	 * Sets the total amount of this record to copies
	 * 
	 * @param copies
	 *            the number of copies to set to
	 */
	public void setTotal(int copies) {
		this.total = copies;
	}

	/**
	 * Sets the rental status of the DVD record
	 * 
	 * @param rental
	 *            true if DVD is for rent, false if for sale
	 */
	public void setRentalStatus(boolean rental) {
		this.rentalStatus = rental;
	}

	/**
	 * Sets the price of the DVD record
	 * 
	 * @param price
	 *            the price in cents
	 */
	public void setPriceInCents(int price) {
		this.priceInCents = price;
	}

	/**
	 * Returns the DVD as a string, formatted for use in the front end
	 * 
	 * @return line within the output DVD File
	 */
	public String toDVDFileString() {
		final int DVD_NAME_SIZE = 25;
		final int DVD_NUMBER_SIZE = 4;
		final int DVD_STATUS_SIZE = 1;
		final int DVD_PRICE_SIZE = 6;

		StringBuilder sb = new StringBuilder();

		sb.append(FormatUtil.padEndWithSpaces(name, DVD_NAME_SIZE));
		sb.append(' ');
		sb.append(FormatUtil.padFrontWithZeros(remain, DVD_NUMBER_SIZE));
		sb.append(' ');
		sb.append(rentalStatus ? "R" : "S");
		sb.append(' ');
		sb.append(FormatUtil.padFrontWithZeros(priceInCents / 100,
				DVD_PRICE_SIZE - 3)
				+ '.'
				+ FormatUtil.padFrontWithZeros(priceInCents % 100,
						DVD_PRICE_SIZE - 4));

		return sb.toString();
	}

	/**
	 * Returns the DVD as a string, formatted for use in the Master DVD File
	 * 
	 * @return line within the new Master DVD File
	 */
	public String toMasterDVDFileString() {
		final int DVD_ID_SIZE = 5;
		final int DVD_NAME_SIZE = 25;
		final int DVD_NUMBER_SIZE = 4;
		final int DVD_TOTAL_SIZE = 4;
		final int DVD_STATUS_SIZE = 1;
		final int DVD_PRICE_SIZE = 6;

		StringBuilder sb = new StringBuilder();

		sb.append(FormatUtil.padFrontWithZeros(id, DVD_ID_SIZE));
		sb.append(' ');
		sb.append(FormatUtil.padFrontWithZeros(remain, DVD_NUMBER_SIZE));
		sb.append(' ');
		sb.append(FormatUtil.padFrontWithZeros(total, DVD_TOTAL_SIZE));
		sb.append(' ');
		sb.append(rentalStatus ? "R" : "S");
		sb.append(' ');
		sb.append(FormatUtil.padFrontWithZeros(priceInCents / 100,
				DVD_PRICE_SIZE - 3)
				+ '.'
				+ FormatUtil.padFrontWithZeros(priceInCents % 100,
						DVD_PRICE_SIZE - 4));
		sb.append(' ');
		sb.append(FormatUtil.padEndWithSpaces(name, DVD_NAME_SIZE));

		return sb.toString();
	}

	/**
	 * Returns a new DVD record created from a string in the transaction file
	 * @return new record
	 */
	static public DVDRecord createRecordFromLine(String line) {
		// System.out.println("Creating from line " + line);
		final int DVD_ID_SIZE = 5;
		final int DVD_NAME_SIZE = 25;
		final int DVD_NUMBER_SIZE = 4;
		final int DVD_TOTAL_SIZE = 4;
		final int DVD_STATUS_SIZE = 1;
		final int DVD_PRICE_SIZE = 6;
		int start_index = 0;
		final int EXPECTED_SIZE = DVD_ID_SIZE + 1
				+ DVD_NUMBER_SIZE + 1 + DVD_TOTAL_SIZE + 1 + DVD_STATUS_SIZE
				+ 1 + DVD_PRICE_SIZE + 1 + DVD_NAME_SIZE;
		if (line.length() != EXPECTED_SIZE) {
			System.out.println("Expected " + EXPECTED_SIZE
					+ " character lines in DVD file.");
			return null;
		}
		String id = line.substring(start_index, start_index + DVD_ID_SIZE);
		start_index += DVD_ID_SIZE + 1;

		// Read in DVD amount (XXX)
		String number = line.substring(start_index, start_index
				+ DVD_NUMBER_SIZE);
		start_index += DVD_NUMBER_SIZE + 1;

		String total = line
				.substring(start_index, start_index + DVD_TOTAL_SIZE);
		start_index += DVD_TOTAL_SIZE + 1;

		// Read in DVD status (R or S)
		String status = line.substring(start_index, start_index
				+ DVD_STATUS_SIZE);
		start_index += DVD_STATUS_SIZE + 1;

		// Read in price (XXX.XX)
		String price = line
				.substring(start_index, start_index + DVD_PRICE_SIZE);
		int priceInCents = (int) Double.parseDouble(price) * 100;
		start_index += DVD_PRICE_SIZE + 1;

		// Read in DVD name (25 characters)
		String name = line.substring(start_index, start_index + DVD_NAME_SIZE);
		name = name.trim();// Remove unnecessary whitespace
		start_index += DVD_NAME_SIZE + 1;

		boolean isRental = status.equals("R");

		// Return DVDRecord with currentSession set to false since record was read
		// in from file
		return new DVDRecord(Integer.parseInt(id), Integer.parseInt(number),
				Integer.parseInt(total), isRental, priceInCents, name);
	}

}
